package com.jwell.panrank.tools;

import android.app.Activity;

import java.lang.ref.WeakReference;
import java.util.Stack;

/**
 * activity管理工具
 */
public class ActivityManager {
    private static WeakReference<ActivityManager> instance;
    private Stack<Activity> activityStack;//activity栈

    private ActivityManager() {

    }

    //单例模式
    public static ActivityManager getInstance() {
        if (instance == null || instance.get() == null) {
            instance = new WeakReference(new ActivityManager());
        }
        return instance.get();

//        return ActivityManagerHolder.ACTIVITY_MANAGER;
    }

//    private static class ActivityManagerHolder {
//        private static final ActivityManager ACTIVITY_MANAGER = new ActivityManager();
//    }

    //把一个activity压入栈中
    public void pushOneActivity(Activity actvity) {
        if (activityStack == null) {
            activityStack = new Stack();
        }
        activityStack.add(actvity);
    }

    //获取栈顶的activity，先进后出原则
    public Activity getLastActivity() {
        return activityStack.lastElement();
    }

    //移除一个activity
    public void popOneActivity(Activity activity) {
        if (activityStack != null && activityStack.size() > 0) {
            if (activity != null) {
                activity.finish();
                activityStack.remove(activity);
                activity = null;
            }
        }
    }

    //退出所有activity
    public void finishAllActivity() {
        if (activityStack != null) {
            while (activityStack.size() > 0) {
                Activity activity = getLastActivity();
                if (activity == null) {
                    break;
                }
                popOneActivity(activity);
            }
        }
    }
}
